package hk.dao.impl;

import hk.dao.DaoRole;
import hk.jdbc.DBUtil;
import hk.pojo.AdminInfo;
import hk.pojo.RoleInfo;
import hk.util.PageInfo;

import java.util.List;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/4/20 21:53
 */
public class DaoRoleImpl implements DaoRole {
    @Override
    public List<RoleInfo> getRoleList() {
        return DBUtil.getList("select * from roleinfo",RoleInfo.class);
    }

    @Override
    public RoleInfo searchForUpdate(String id) {
        return DBUtil.getSingleObj("select * from roleinfo where id = ? ",RoleInfo.class,id);
    }

    @Override
    public void updateMsg(int id, String roleName, String des) {
        DBUtil.update("update roleinfo set roleName = ?,des = ? where id = ?",roleName,des,id);
    }

    @Override
    public int deleteMsg(String id) {
        return DBUtil.update("delete from roleinfo where id = ?",id);
    }

    @Override
    public String  getRoleMenuIdStr(int roleId) {
        String sql="select menuId from roleMenu where roleId=?";
        List<Integer> menuIdList = DBUtil.getColumn(sql, roleId);

        StringBuffer buff=new StringBuffer();

        for(int i=0;i<menuIdList.size();i++) {
            if(i<menuIdList.size()-1) {
                buff.append(menuIdList.get(i)+",");
            }
            else {
                buff.append(menuIdList.get(i));
            }
        }

        return buff.toString();
    }

    @Override
    public RoleInfo getRoleInfoById(int id) {
        return DBUtil.getSingleObj("select * from roleinfo where id = ? limit 1",RoleInfo.class,id);
    }

    @Override
    public void updateRoleMenu(int roleId, String[] menuIdList) {
        DBUtil.update("delete from roleMenu where roleId=?", roleId);
        for(String menuId: menuIdList) {
            DBUtil.update("insert into roleMenu (roleId,menuId) values (?,?) " , roleId,menuId);
        }
    }

    @Override
    public AdminInfo selectByDoubleTableForMsg(String id) {
        return DBUtil.getSingleObj("select a.*, r.roleName from admininfo a left join roleinfo r on a.roleId = r.id where a.id = ?",AdminInfo.class,id);
    }

    @Override
    public List<AdminInfo> selectByDoubleTableAndAnyoneForMsg() {
        return DBUtil.getList("select a.*, r.roleName from admininfo a left join roleinfo r on a.roleId = r.id",AdminInfo.class);
    }

    @Override
    public List<AdminInfo> getAdminList(PageInfo pageInfo) {
        return DBUtil.getList("select a.*, r.roleName from admininfo a left join roleinfo r on a.roleId = r.id where state != '0' limit ?,?",AdminInfo.class,pageInfo.getBeginRow(),pageInfo.getPageSize());

    }

    @Override
    public int getAdminCount() {
        long adminCount = DBUtil.getScala("select count(*) from admininfo where state != '0'");
        return Integer.parseInt(adminCount+"");
    }

    @Override
    public int updateByAdmin(AdminInfo adminInfo) {
        return DBUtil.update("update adminInfo set roleId = ? where id = ?",adminInfo.getRoleId(),adminInfo.getId());
    }

    @Override
    public int add(String roleName, String des) {
        return DBUtil.addAndReturnId("insert into roleinfo (roleName,des) values (?,?)",roleName,des);
    }

    @Override
    public RoleInfo selectOneMsg(int id) {
        return DBUtil.getSingleObj("select * from roleinfo where id = ?",RoleInfo.class,id);
    }
}
